#!/bin/bash

# TZAI H5 项目自动部署脚本
# 部署到阿里云服务器

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 配置信息
PROJECT_ROOT="/Users/zhaolin/Documents/Lin/接单/TZAI"
H5_DIR="$PROJECT_ROOT/tzai-h5"
SERVER_IP="39.97.249.188"
SERVER_USER="root"
SERVER_PASSWORD="@20Shagua"
SERVER_PATH="/usr/share/nginx/html/tzai-h5"
ACCESS_URL="http://39.97.249.188:8666/tzai-h5/"

echo "=========================================="
echo "  TZAI H5 自动部署脚本"
echo "=========================================="
echo ""
echo "目标服务器: $SERVER_IP"
echo "部署路径: $SERVER_PATH"
echo "访问地址: $ACCESS_URL"
echo ""

# 1. 检查项目目录
print_info "[1/5] 检查项目目录..."
if [ ! -d "$H5_DIR" ]; then
    print_error "项目目录不存在: $H5_DIR"
    exit 1
fi
cd "$H5_DIR"
print_success "项目目录检查通过"

# 2. 检查依赖
print_info "[2/5] 检查 node_modules..."
if [ ! -d "node_modules" ]; then
    print_warning "node_modules 不存在，正在安装依赖..."
    npm install
    if [ $? -eq 0 ]; then
        print_success "依赖安装成功"
    else
        print_error "依赖安装失败"
        exit 1
    fi
else
    print_success "依赖已存在"
fi

# 3. 打包项目
print_info "[3/5] 打包项目..."
npm run build

if [ $? -eq 0 ]; then
    print_success "项目打包成功"
else
    print_error "项目打包失败"
    exit 1
fi

# 检查 dist 目录
if [ ! -d "dist" ]; then
    print_error "打包目录 dist 不存在"
    exit 1
fi

# 显示打包信息
echo ""
print_info "打包文件列表:"
ls -lh dist/
echo ""

# 4. 上传到服务器
print_info "[4/5] 上传文件到服务器..."
print_info "备份旧文件..."

# 先备份旧文件
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP \
    "mkdir -p /root/backups/tzai-h5 && \
     cp -r $SERVER_PATH/* /root/backups/tzai-h5/backup_$(date +%Y%m%d_%H%M%S)/ 2>/dev/null || true"

print_info "上传新文件..."
sshpass -p "$SERVER_PASSWORD" scp -r -o StrictHostKeyChecking=no dist/* $SERVER_USER@$SERVER_IP:$SERVER_PATH/

if [ $? -eq 0 ]; then
    print_success "文件上传成功"
else
    print_error "文件上传失败"
    exit 1
fi

# 5. 重新加载 Nginx
print_info "[5/5] 重新加载 Nginx..."
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP \
    "nginx -t && systemctl reload nginx"

if [ $? -eq 0 ]; then
    print_success "Nginx 重新加载成功"
else
    print_error "Nginx 重新加载失败"
    exit 1
fi

# 验证部署
echo ""
print_info "验证部署..."
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP \
    "ls -lh $SERVER_PATH/ && echo '' && echo '文件总数:' && find $SERVER_PATH -type f | wc -l"

echo ""
echo "=========================================="
echo "  部署完成"
echo "=========================================="
echo ""
print_success "TZAI H5 已成功部署到服务器！"
echo ""
echo "访问地址:"
echo "  🌐 主页: $ACCESS_URL"
echo "  📱 建议使用移动设备或浏览器移动模式访问"
echo ""
echo "常用命令:"
echo "  查看日志: sshpass -p '$SERVER_PASSWORD' ssh $SERVER_USER@$SERVER_IP 'tail -f /var/log/nginx/access.log'"
echo "  查看错误: sshpass -p '$SERVER_PASSWORD' ssh $SERVER_USER@$SERVER_IP 'tail -f /var/log/nginx/error.log'"
echo "  重启 Nginx: sshpass -p '$SERVER_PASSWORD' ssh $SERVER_USER@$SERVER_IP 'systemctl restart nginx'"
echo ""
print_info "备份文件保存在服务器: /root/backups/tzai-h5/"
echo ""

